﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data; 
using System.Data.Common;
using WMSADBUtility;
using WMSAML.SY;

namespace WMSADL.SY
{
    public class TGroup_DAL
    {
        /// <summary>
        /// 
        /// </summary>
        public int Add(TGroup_Entity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into TGroup(");
            strSql.Append("Parent_id,Group_name,Creat_time,Description)");

            strSql.Append(" values (");
            strSql.Append("@Parent_id,@Group_name,@Creat_time,@Description)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "Parent_id", DbType.Int32, entity.Parent_id);
            db.AddInParameter(dbCommand, "Group_name", DbType.String, entity.Group_name);
            db.AddInParameter(dbCommand, "Creat_time", DbType.DateTime, entity.Creat_time);
            db.AddInParameter(dbCommand, "Description", DbType.String, entity.Description);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM TGroup WHERE Group_id=@Group_id ");
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "Group_id", DbType.Int32, id);
                int rows = db.ExecuteNonQuery(dbCommand);
                if (rows > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(TGroup_Entity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE TGroup SET ");
            strSql.Append("Group_name=@Group_name,");
            strSql.Append("Parent_id=@Parent_id,");
            strSql.Append("Creat_time=@Creat_time,");
            strSql.Append("Description=@Description");
            strSql.Append(" WHERE Group_id=@Group_id ");
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "Group_id", DbType.Int32, entity.Group_id);
                db.AddInParameter(dbCommand, "Group_name", DbType.String, entity.Group_name);
                db.AddInParameter(dbCommand, "Parent_id", DbType.Int32, entity.Parent_id);
                db.AddInParameter(dbCommand, "Creat_time", DbType.DateTime, entity.Creat_time);
                db.AddInParameter(dbCommand, "Description", DbType.String, entity.Description);
                int rows = db.ExecuteNonQuery(dbCommand);

                if (rows > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        /// <summary>
        /// ByID
        /// </summary>
        public bool ExistsByID(int id)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(1) FROM TGroup WHERE Group_id=@Group_id ");
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "Group_id", DbType.Int32, id);
                int cmdresult;
                object obj = db.ExecuteScalar(dbCommand);
                if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                {
                    cmdresult = 0;
                }
                else
                {
                    cmdresult = int.Parse(obj.ToString());
                }
                if (cmdresult == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        /// <summary>
        /// ByName
        /// </summary>
        public bool ExistsByName(string group_name)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(1) FROM TGroup WHERE Group_name=@Group_name ");
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "Group_name", DbType.String, group_name);
                int cmdresult;
                object obj = db.ExecuteScalar(dbCommand);
                if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                {
                    cmdresult = 0;
                }
                else
                {
                    cmdresult = int.Parse(obj.ToString());
                }
                if (cmdresult == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        /// <summary>
        /// ByName
        /// </summary>
        /// <summary>
        public TGroup_Entity GetEntityByName(string group_name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT Group_id,Group_name,Parent_id,Creat_time,Description FROM TGroup ");
            strSql.Append(" WHERE Group_name=@Group_name ");
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "Group_name", DbType.String, group_name);
                TGroup_Entity entity = new TGroup_Entity();
                using (IDataReader dataReader = db.ExecuteReader(dbCommand))
                {
                    if (dataReader.Read())
                    {
                        entity.Group_id = Convert.ToInt32(dataReader["Group_id"].ToString());
                        entity.Parent_id = Convert.ToInt32(dataReader["Parent_id"].ToString());
                        entity.Group_name = dataReader["Group_name"].ToString();
                        entity.Creat_time = Convert.ToDateTime(dataReader["Creat_time"].ToString());
                        entity.Description = dataReader["Description"].ToString();
                    }
                }
                return entity;
            }
        }

        /// <summary>
        /// ByID
        /// </summary>
        /// <summary>
        public TGroup_Entity GetEntityByID(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT Group_id,Group_name,Parent_id,Creat_time,Description FROM TGroup ");
            strSql.Append(" WHERE Group_id=@Group_id ");
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "Group_id", DbType.Int32, id);
                TGroup_Entity entity = new TGroup_Entity();
                using (IDataReader dataReader = db.ExecuteReader(dbCommand))
                {
                    if (dataReader.Read())
                    {
                        entity.Group_id = Convert.ToInt32(dataReader["Group_id"].ToString());
                        entity.Parent_id = Convert.ToInt32(dataReader["Parent_id"].ToString());
                        entity.Group_name = dataReader["Group_name"].ToString();
                        entity.Creat_time = Convert.ToDateTime(dataReader["Creat_time"].ToString());
                        entity.Description = dataReader["Description"].ToString();
                    }
                }
                return entity;
            }
        }

    }
}
